package com.ruoyi.web.controller.fx;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.fx.FxStore;
import com.ruoyi.system.domain.fx.FxToJoin;
import com.ruoyi.system.service.fx.IFxStoreService;
import com.ruoyi.system.service.fx.IFxToJoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 加盟Controller
 *
 * @author ruoyi
 * @date 2024-08-19
 */
@RestController
@RequestMapping("/fx/join")
public class FxToJoinController extends BaseController {
    @Autowired
    private IFxToJoinService fxToJoinService;
    @Autowired
    private IFxStoreService fxStoreService;
    /**
     * 查询加盟列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FxToJoin fxToJoin) {
        startPage();
        List<FxToJoin> list = fxToJoinService.selectFxToJoinList(fxToJoin);
        return getDataTable(list);
    }

    /**
     * 导出加盟列表
     */
    @Log(title = "加盟", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxToJoin fxToJoin) {
        List<FxToJoin> list = fxToJoinService.selectFxToJoinList(fxToJoin);
        ExcelUtil<FxToJoin> util = new ExcelUtil<FxToJoin>(FxToJoin.class);
        util.exportExcel(response, list, "加盟数据");
    }

    /**
     * 获取加盟详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxToJoinService.selectFxToJoinById(id));
    }

    /**
     * 新增加盟 (小程序加盟 3供应商，4店铺)
     */
    @Log(title = "加盟", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxToJoin fxToJoin) {
        return toAjax(fxToJoinService.insertFxToJoin(fxToJoin));
    }

    /**
     * 去查询申请情况
     * @return
     */
    @GetMapping("/toApply")
    public AjaxResult toApply(Integer type){
        FxToJoin fxToJoin = fxToJoinService.selectToJoin(type);
        if (StringUtils.isNotNull(fxToJoin)) {
        return AjaxResult.success(fxToJoin);
        }
        return AjaxResult.error();
    }

    /**
     * 修改加盟
     */
    @Log(title = "加盟", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxToJoin fxToJoin) {
        return toAjax(fxToJoinService.updateFxToJoin(fxToJoin));
    }


    @Log(title = "加盟审核", businessType = BusinessType.UPDATE)
    @PostMapping("/updateFxToJoinWithEasy")
    public AjaxResult updateFxToJoinWithEasy(@RequestBody FxToJoin fxToJoin) {
//        fxToJoin=fxToJoinService.selectFxToJoinById(fxToJoin.getId());
//        if(fxToJoin.getStoreId()!=null){
//            FxStore fxStore=new FxStore();
//            fxStore.setId(fxToJoin.getStoreId());
//            fxStore.setStatus(Long.valueOf(fxToJoin.getStatus()));
//            fxStoreService.updateFxStore(fxStore);
//        }
        return toAjax(fxToJoinService.updateFxToJoinWithEasy(fxToJoin));
    }

    /**
     * 删除加盟
     */
    @Log(title = "加盟", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fxToJoinService.deleteFxToJoinByIds(ids));
    }

    /**
     * 查询供应商列表（下拉框用）
     * @return
     */
    @GetMapping("/getSupplierList")
    public AjaxResult getSupplierList() {
        return success(fxToJoinService.getSupplierList());
    }
}
